# -*- coding: utf-8 -*-

# @Author  : Acm
import json

import jsonpath
import requests

base_url = "https://unsplash.com/napi/search/photos?"

headers = {"User-Agent": "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Mobile Safari/537.36"}

j = 1
# 链接地址
url_list = []

# 加参数
for i in range(10):
    params = {
        "query": "flower",
        "xp": "",
        "per_page": 20,
        "page": i,
    }

    # 请求数据
    response = requests.get(url=base_url, headers=headers, params=params)
    html_str = response.content
    jsonobj = json.loads(html_str)

    # 每页20条链接数据进入列表
    for i in range(20):
        small_url_list = jsonpath.jsonpath(jsonobj, "$.results[{}].urls.small".format(i))[0].encode('utf-8')
        url_list.append(small_url_list)

# 列表去重
url_list = list(set(url_list))

# 写到文件中
for i in url_list:
    res = requests.get(url=i, headers=headers, stream=True)
    with open('./img/flower_'+str(j)+'.jpg', 'wb') as f:
        for data in res.iter_content(128):
            f.write(data)
        j += 1
        